package com.tianqu.tms.www.transfer.action;

import com.tianqu.LicensePlateConfigConstant;
import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAjaxAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.entity.TransferProcessHistory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class TransferGetResultPlaneAction extends TmsAjaxAction {

	private Logger logger = Logger.getLogger(TransferRgAjaxAction.class);
	public static final String SERIAL = "serial";
	public static final String LICENSE = "license";
	public static final String OWNERNAME = "ownerName";

	@Override
	public void process(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws TmsException {
		logger.debug(this.getClass().getName() + "#execute was called.");
		String serial = request.getParameter(SERIAL);
		String license = request.getParameter(LICENSE);
		
		TransferAcceptedInfo accept = new TransferAcceptedInfo();
		if(license!=null && !"".equals(license))
		{
			license = LicensePlateConfigConstant.defLicensePlate+license;
		}
		accept = accept.getAcceptInfoBySerial(serial, license);
		String str = "";
		String str1 = "无此车辆过户信息！";
		String str2 = "0";
		if(accept!=null)
		{
			TransferProcessHistory history = new TransferProcessHistory();
			List list = history.loadtHistoryBySerial(accept.getSerial());
			
			//根据历史记录状态查询当前车辆信息
			if(accept.getStatus() == 16 || accept.getStatus() == 18)
			{
				for(int i=0;i<list.size();i++)
				{
					TransferProcessHistory entity = (TransferProcessHistory)list.get(i);
					if(accept.getStatus() == 16)
					{
						if(entity.getBusinessType()==39)
						{
							str1 = "无此车辆过户信息！";
							break;
						}
						if(entity.getBusinessType()==11)
						{
							str1 = "已制证";
							break;	
						}
						if(entity.getBusinessType() == 21)
						{
							str1 = "审档退办";
							if(entity.getReason()!=null)
							{
								str2 =entity.getReason();
							}
							break;	
						}
						if(entity.getBusinessType() == 26)
						{
							str1 = "其他退办";
							if(entity.getReason()!=null)
							{
								str2 =entity.getReason();
							}							
							break;	
						}
						if(entity.getBusinessType() == 36)
						{
							str1 = "凭证退办";
							if(entity.getReason()!=null)
							{
								str2 =entity.getReason();
							}							
							break;	
						}
					}
					if(accept.getStatus() == 18)
					{
						if(entity.getBusinessType() == 20)
						{
							str1 = "审档退办";
							if(entity.getReason()!=null)
							{
								str2 =entity.getReason();
							}							
							break;	
						}
						if(entity.getBusinessType() == 22)
						{
							str1 = "凭证退办";
							if(entity.getReason()!=null)
							{
								str2 =entity.getReason();
							}							
							break;	
						}
						if(entity.getBusinessType() == 25)
						{
							str1 = "其他退办";
							if(entity.getReason()!=null)
							{
								str2 =entity.getReason();
							}							
							break;	
						}
					}
				}
			}else{
				Codes code = Codes.getInstance();
				str1 = code.getItemValueByItemIdAndItemCode(Codes.SEARCH_ACCEPT_TYPE, accept.getStatus());
			}
			
//			TransferProcessHistory history = new TransferProcessHistory();
//			List list = history.searchtHistoryBySerial(accept.getSerial());
//			if(list!=null&&list.size()>0)
//			{
//				for(int i=0;i<list.size();i++)
//				{
//					TransferProcessHistory entity = (TransferProcessHistory)list.get(0);
//					if(entity.getBusinessType() >= 0)
//					{
//						str1 = entity.getBusinessTypeS();
////						if(accept.getStatus()==18||accept.getStatus()==21||accept.getStatus()==22||accept.getStatus()==26)
////						{
////							str2 =entity.getReason();
////						}
//						//20：审档退办，21：审档退办退费，22：凭证退办，25：其他退办 26：其他退办退费 29：查验未通过，32：工商查验未通过 36：凭证退办退费，
//						if(entity.getBusinessType()==20||entity.getBusinessType()==21||entity.getBusinessType()==22||entity.getBusinessType()==25||entity.getBusinessType()==26||entity.getBusinessType()==29||entity.getBusinessType()==32||entity.getBusinessType()==36)
//						{
//							str2 =entity.getReason();
//						}
//					}
//				}
//			}
		}
		str = str1+","+str2;
		String responseText = str.toString();
		responseValues(response, responseText, TEXT);
		logger.debug(this.getClass().getName() + "#execute was completed.");

	}
}
